<?php
class Text 
{
    static function daddslashes(&$array) 
    {
        foreach($array as $key => $value) 
        {
            if(!is_array($value)) 
            {
                $array[$key] = addslashes($value);
            }
            else 
            {
                self::daddslashes($array[$key]);
            }
        }
    }

    static function dhtmlspecialchars($string) 
    {
        if(is_array($string)) 
        {
            foreach($string as $key => $val) 
            {
                $string[$key] = self::dhtmlspecialchars($val);
            }
        }
        else
        {
            $string = preg_replace('/&amp;((#(\d{3,5}|x[a-fA-F0-9]{4})|[a-zA-Z][a-z0-9]{2,5});)/', '&\\1',
            str_replace(array('&', '"', '<', '>'), array('&amp;', '&quot;', '&lt;', '&gt;'), $string));
        }
        return $string;
    }

    /**
    * Make text safe to output into DOUBLE-QUOTE enclosed attirbutes of an HTML tag
    */
    static function formSafe( $txt, $deslash=false ) 
    {

        if (is_array( $txt ))
        {
            foreach ($txt as $k=>$v)
            {
                if ($deslash)
                {
                    $txt[$k] = htmlspecialchars( stripslashes( $v ), ENT_QUOTES);
                }
                else
                {
                    $txt[$k] = htmlspecialchars( $v , ENT_QUOTES);
                }
            }
        } 
        else
        {
            if ($deslash)
            {
                $txt = htmlspecialchars( stripslashes( $txt ) ,ENT_QUOTES);
            }
            else 
            {
                $txt = htmlspecialchars( $txt , ENT_QUOTES);
            }
        }
        return $txt;
    }


    static function safeSql($txt)
    {
        if (is_array($txt))
        {
            foreach ($txt as $k => $v)
            {
                if (is_array($v))
                {
                    foreach ($v as $kk => $vv)
                    {
                        $v[$kk] = mysql_escape_string($vv);
                    }
                    $txt[$k] = $v;
                }
                else
                {
                    $txt[$k] = mysql_escape_string($v);
                }
            }
        }
        else
        {
            $txt = mysql_escape_string($txt);
        }
        return $txt;
    }

    static function getAuthKey($club_urlname, $post_id)
    {
        return md5($club_urlname."_".CLUB_TOPIC_AUTH_KEY."_".$post_id);
    }

    static function userJs($username)
    {
        $hash = md5($username);
        $chr1 = substr($hash, 0, 1);

        switch($chr1)
        {
            case "0":  case "1":
                $host = 'http://j1.51.com/';    break;
            case "2":  case "3":
                $host = 'http://j2.51.com/';    break;
            case "4":  case "5":
                $host = 'http://j3.51.com/';    break;
            case "6":  case "7":
                $host = 'http://j4.51.com/';    break;
            case "8":  case "9":
                $host = 'http://j5.51.com/';    break;
            case "a": case "b":
                $host = 'http://j6.51.com/';    break;
            case "c": case "d":
                $host = 'http://j7.51.com/';    break;
            case "e": case "f":
                $host = 'http://j8.51.com/';    break;
            default:
                $host = '';
        }

        return $host.substr($hash, 1, 2).'/'.substr($hash, 3,2).'/'.$username.'.js';
    }

    // 加入俱乐部时添加的验证代码
    static function genEncryptCode($username, $ret_hash = true)
    {
        if (empty($username)) { return; }
        $hash = sha1($username);
        $hash = md5(substr($hash, 23, 4).substr($hash, 10, 3).substr($hash, 1, 5));

        return ($ret_hash ? $hash : 'ecode='.$hash);
    }

    static function getUploadPhotoUrl($club_urlname, $username) 
    {
        $time = time() ;
        $key  = md5( md5($username) . md5("xxxxxxxxxxxxxx") . md5($time));

        $referurl = DOMAIN_URL.$club_urlname."/loadImageObj.ubb.php" ;
        $url = "http://photo.51.com/photo/ImageUploadInterface.php?user={$username}&time={$time}&refer={$referurl}&key={$key}" ;

        return $url;
    }

    static function txtIconv($str,$in="UTF8",$out="GBK") 
    {
        if (function_exists('mb_convert_encoding')) 
        {
            return mb_convert_encoding($str, $out, $in);
        }
        elseif (function_exists('iconv')) 
        {
            return iconv($in, $out, $str);
        }
    }
    //过滤掉一些标签
    static function txtClear($message)
    {
        $searcharray=
        array('[u]','[/u]','[b]','[/b]','[i]','[/i]','[list]','[li]','[/li]','[/list]','[strike]','[/strike]','[blockquote]','[/blockquote]','[hr]','[/backcolor]', '[/color]','[/font]','[/size]','[/align]'
        );
        #echo $str,"\n";
        $str = str_replace($searcharray, '', $message);

        #echo $str;

        $searcharray = array(
            "/\[font=([^\[\(&]+?)\]/is",
            "/\[color=([#0-9a-z]{1,10})\]/is",
            "/\[backcolor=([#0-9a-z]{1,10})\]/is",
            "/\[size=(\d+)\]/eis",
            "/\[align=(left|center|right|justify)\]/is",
        );
        $str = preg_replace($searcharray, '', $str);
        
        $str = preg_replace('/\s+/', '', $str);
        return $str;
    }
    static function txtConvert($message,$is_sign=false)
    {
        //$message = preg_replace("'\n'", "<br/>", $message);
        $searcharray = array("/\n/","/\s/");
        $replacearray = array("<br/>","&nbsp;");
        $message = preg_replace($searcharray,$replacearray,$message);
        $message = preg_replace('/\[list=([aA1]?)\](.+?)\[\/list\]/is', "<ol type=\"\\1\" style=\"margin:0 0 0 25px\">\\2</ol>", $message);
        
        $searcharray = array('[u]','[/u]','[b]','[/b]','[i]','[/i]','[list]','[li]','[/li]','[/list]','[strike]','[/strike]','[blockquote]','[/blockquote]','[hr]','[/backcolor]', '[/color]','[/font]','[/size]','[/align]'
        );
        $replacearray = array('<u>','</u>','<b>','</b>','<i>','</i>','<ul style="margin:0 0 0 15px">','<li>', '</li>','</ul>','<strike>','</strike>','<blockquote>','</blockquote>', '<hr />','</span>','</span>','</span>','</font>','</div>'
        );
        $message = str_replace($searcharray,$replacearray,$message);

        $searcharray = array(
            "/\[font=([^\[\(&]+?)\]/is",
            "/\[color=([#0-9a-z]{1,10})\]/is",
            "/\[backcolor=([#0-9a-z]{1,10})\]/is",
            "/\[size=(\d+)\]/eis",
            "/\[align=(left|center|right|justify)\]/is",
        );
        $replacearray = array(
            "<span style=\"font-family:\\1 \">",
            "<span style=\"color:\\1 \">",
            "<span style=\"background-color:\\1 \">",
            "self::fontSize('\\1')",
            "<div align=\"\\1\">",
        );
        $message = preg_replace($searcharray, $replacearray, $message);
		if($is_sign === false)
		{
			$message = preg_replace("/\[img\](.+?)\[\/img\]/eis", "self::cvPic('\\1')", $message);
		}
		else
		{
			$message = preg_replace("/\[img\](.+?)\[\/img\]/eis", "self::resizePic('\\1')", $message);
		}
		
		$message = preg_replace("/\[img=(face\d\/\d+).gif\]/", "<img src=\"http://static.51img1.com/game/img/face/\\1.gif\"\>", $message);
		//$message = preg_replace("/\[flash * \](.+?)\[\/flash\]/eis","<OBJECT CLASSID=\"clsid:D27CDB6E-AE6D-11cf-96B8-444553540000\" WIDTH=\\2 HEIGHT=\\4><PARAM NAME=MOVIE VALUE=\\6><PARAM NAME=PLAY VALUE=TRUE><PARAM NAME=LOOP VALUE=TRUE><PARAM NAME=QUALITY VALUE=HIGH><PARAM NAME=allowFullScreen VALUE=false><param name=\"wmode\"   value=\"transparent\"><PARAM NAME=allownetworking VALUE=internal><PARAM NAME=allowscriptaccess VALUE=never><EMBED SRC=\\6 WIDTH=\\2 HEIGHT=\\4 PLAY=TRUE LOOP=TRUE QUALITY=HIGH allowscriptaccess='never' allownetworking='internal' allowFullScreen='false'></EMBED></OBJECT><br />[<a target=_blank href=\\6>全屏播放</a>]",$message);
        $searcharray = array("/\[flash=(\d+?)\,(\d+?)(\,(0|1))?\](.+?)\[\/flash\]/is",
        					"/\[mp3=(\d+?)\,(\d+?)(\,(0|1))?\](.+?)\[\/mp3\]/is",
        					"/\[rm=(\d+?)\,(\d+?)(\,(0|1))?\](.+?)\[\/rm\]/is",
        					"/\[wmv=(\d+?)\,(\d+?)(\,(0|1))?\](.+?)\[\/wmv\]/is"
        );
        $replacearray = array("<OBJECT CLASSID=\"clsid:D27CDB6E-AE6D-11cf-96B8-444553540000\" WIDTH=\\1 HEIGHT=\\2><PARAM NAME=MOVIE VALUE=\\5><PARAM NAME=PLAY VALUE=TRUE><PARAM NAME=LOOP VALUE=TRUE><PARAM NAME=QUALITY VALUE=HIGH><PARAM NAME=IsAutoPlay VALUE=\\4><PARAM NAME=allowFullScreen VALUE=false><param name=\"wmode\"   value=\"transparent\"><PARAM NAME=allownetworking VALUE=internal><PARAM NAME=allowscriptaccess VALUE=never><EMBED SRC=\\5 WIDTH=\\1 HEIGHT=\\2 PLAY=TRUE LOOP=TRUE QUALITY=HIGH allowscriptaccess='never' allownetworking='internal' allowFullScreen='false'></EMBED></OBJECT><br />[<a target=_blank href=\\5>全屏播放</a>]",
	        				"<center><object id='mPlayer1' width=468 height=64 classid='CLSID:6BF52A52-394A-11D3-B153-00C04F79FAA6'><param name='invokeURLs' value='0'><param name='URL' value='\\5'><param name='rate' value='1'><param name='balance' value='0'><param name='currentPosition' value='0'><param name='defaultFrame' value><param name='playCount' value='100'><param name='autoStart' value='true'><param name='currentMarker' value='0'><param name='invokeURLs' value='-1'><param name='baseURL' value><param name='volume' value='100'><param name='mute' value='0'><param name='uiMode' value='full'><param name='stretchToFit' value='0'><param name='windowlessVideo' value='0'><param name='enabled' value='-1'><param name='enableContextMenu' value='-1'><param name='fullScreen' value='0'><param name='SAMIStyle' value><param name='SAMILang' value><param name='SAMIFilename' value><param name='captioningID' value><param name='enableErrorDialogs' value='0'><param name='_cx' value='7779'><param name='_cy' value='1693'></object><br><font style='font-size:9pt' color=black>如果不能正常播放,请下载&nbsp; <img src='http://pic.51.com/images/wmv.gif' align='absmiddle'>&nbsp;<a href='http://www.microsoft.com/windows/windowsmedia/download/default.asp'>Windows Media Player</a></font></center>",
	        				"<center><object classid='clsid:22D6F312-B0F6-11D0-94AB-0080C74C7E95' height='400'  id='MediaPlayer1' width='550' ><param name='AudioStream' value='-1'><param name='AutoSize' value='0'><param name='AutoStart' value='true'><param name='AnimationAtStart' value='-1'><param name='AllowScan' value='-1'><param name='AllowChangeDisplaySize' value='-1'><param name='AutoRewind' value='-1'><param name='Balance' value='0'><param name='BufferingTime' value='5'><param name='ClickToPlay' value='-1'><param name='CursorType' value='0'><param name='CurrentPosition' value='-1'><param name='CurrentMarker' value='0'><param name='DisplayBackColor' value='0'><param name='DisplayForeColor' value='16700215'><param name='DisplayMode' value='0'><param name='DisplaySize' value='2'><param name='Enabled' value='-1'><param name='EnableContextMenu' value='1'><param name='EnablePositionControls' value='-1'><param name='EnableFullScreenControls' value='0'><param name='EnableTracker' value='-1'><param name='Filename' value='\\5'><param name='InvokeURLs' value='-1'><param name='Language' value='-1'><param name='Mute' value='0'><param name='PlayCount' value='1'><param name='PreviewMode' value='0'><param name='Rate' value='1'><param name='SelectionStart' value='-1'><param name='SelectionEnd' value='-1'><param name='SendOpenStateChangeEvents' value='-1'><param name='SendWarningEvents' value='-1'><param name='SendErrorEvents' value='-1'><param name='SendKeyboardEvents' value='0'><param name='SendMouseClickEvents' value='0'><param name='SendMouseMoveEvents' value='0'><param name='SendPlayStateChangeEvents' value='-1'><param name='ShowCaptioning' value='0'><param name='ShowControls' value='-1'><param name='ShowAudioControls' value='-1'><param name='ShowDisplay' value='0'><param name='ShowGotoBar' value='0'><param name='ShowPositionControls' value='-1'><param name='ShowStatusBar' value='-1'><param name='ShowTracker' value='-1'><param name='TransparentAtStart' value='0'><param name='VideoBorderWidth' value='0'><param name='VideoBorderColor' value='0'><param name='VideoBorder3D' value='0'><param name='Volume' value='-40'><param name='WindowlessVideo' value='0'></object><br><font style='font-size:9pt' color=black>如果不能正常播放,请下载&nbsp; <img src='http://pic.51.com/images/wmv.gif' align='absmiddle'>&nbsp;<a href='http://www.microsoft.com/windows/windowsmedia/download/default.asp'>Windows Media Player</a></font></center>",
        					"<center><object ID='video1' CLASSID='clsid:CFCDAA03-8BE4-11CF-B84B-0020AFBBCCFA' HEIGHT='400' WIDTH='550'><param name='_ExtentX' value='22304'><param name='_ExtentY' value='14288'><param name='AUTOSTART' value='true'><param name='SHUFFLE' value='0'><param name='PREFETCH' value='0'><param name='NOLABELS' value='0'><param name='SRC' value='\\5'><param name='CONTROLS' value='ImageWindow'><param name='CONSOLE' value='Clip1'><param name='LOOP' value='0'><param name='NUMLOOP' value='0'><param name='CENTER' value='0'><param name='MAINTAINASPECT' value='0'><param name='BACKGROUNDCOLOR' value='#000000'></object><object ID='video1' CLASSID='clsid:CFCDAA03-8BE4-11CF-B84B-0020AFBBCCFA' HEIGHT='60' WIDTH='400'><param name='controls' value='ControlPanel,StatusBar'><param name='console' value='Clip1'><embed type='audio/x-pn-realaudio-plugin' CONSOLE='Clip1' CONTROLS='ControlPanel,StatusBar' HEIGHT='60' WIDTH='400' AUTOSTART='true'></object></center>",
	    );
		//$message =preg_replace("/\[flash=(\d+?)\,(\d+?)(\,(0|1))?\](.+?)\[\/flash\]/is","<OBJECT CLASSID=\"clsid:D27CDB6E-AE6D-11cf-96B8-444553540000\" WIDTH=\\1 HEIGHT=\\2><PARAM NAME=MOVIE VALUE=\\5><PARAM NAME=PLAY VALUE=TRUE><PARAM NAME=LOOP VALUE=TRUE><PARAM NAME=QUALITY VALUE=HIGH><PARAM NAME=IsAutoPlay VALUE=\\4><PARAM NAME=allowFullScreen VALUE=false><param name=\"wmode\"   value=\"transparent\"><PARAM NAME=allownetworking VALUE=internal><PARAM NAME=allowscriptaccess VALUE=never><EMBED SRC=\\5 WIDTH=\\1 HEIGHT=\\2 PLAY=TRUE LOOP=TRUE QUALITY=HIGH allowscriptaccess='never' allownetworking='internal' allowFullScreen='false'></EMBED></OBJECT><br />[<a target=_blank href=\\5>全屏播放</a>]",$message);
		$message = preg_replace($searcharray, $replacearray, $message);
		if(strpos($message,'[/URL]')!==false || strpos($message,'[/url]')!==false)
        {
            $searcharray = array(
                "/\[url=(https?|ftp|gopher|news|telnet|mms|rtsp|thunder)([^\[\s]+?)\](.+?)\[\/url\]/eis",           
                "/\[url\]www\.([^\[]+?)\[\/url\]/eis",
                "/\[url\](https?|ftp|gopher|news|telnet|mms|rtsp|thunder)([^\[]+?)\[\/url\]/eis"
            );
            $replacearray = array(
                "self::cvUrl('\\1','\\2','\\3')",
                "self::cvUrl('\\1')",
                "self::cvUrl('\\1','\\2')"
            );
            $message = preg_replace($searcharray, $replacearray, $message);
        }
        if(strpos($message,"[quote]") !== false && strpos($message,"[/quote]") !== false)
        {
            $message = preg_replace("/\[quote\](.+?)\[\/quote\]/eis","self::qoute('\\1')", $message);
        }
        return $message;
    }

	static function ubbConvert($message, $disallowed_tags='', $convert_text=true) {
        //init
        //过滤掉黑名单网站的图片
        
		/*$time_start = microtime_float();
        $filter_domian  = self::getPicFilterDoamin();
        if (is_array($filter_domian)) {
        	foreach ($filter_domian as $k=> $r) {
        		if ($k>29) break;
				$r["domain_url"] = str_replace("/","\\/",$r["domain_url"]);
        		$replacearray[]  = "/\[img\]http:\/\/(?>[^\/]+)\/(?<=".preg_quote($r["domain_url"])."\/)(?>[^\[]+)\[\/img\]/is";
        		$replacearray[]  = "/\[img=http:\/\/(?>[^\/]+)\/(?<=".preg_quote($r["domain_url"])."\/)(?>[^\]]+)\]/is";
        		$replacearray[]  = "/\[flash\]http:\/\/(?>[^\/]+)\/(?<=".preg_quote($r["domain_url"])."\/)(?>[^\[]+)\[\/flash\]/is";
        		$replacearray[]  = "/\[flash=http:\/\/(?>[^\/]+)\/(?<=".preg_quote($r["domain_url"])."\/)(?>[^\]]+)\]/is";
        		$replacearray[]  = "/\[movie\]http:\/\/(?>[^\/]+)\/(?<=".preg_quote($r["domain_url"])."\/)(?>[^\[]+)\[\/movie\]/is";
        		$replacearray[]  = "/\[movie=http:\/\/(?>[^\/]+)\/(?<=".preg_quote($r["domain_url"])."\/)(?>[^\]]+)\]/is";
			}
        }
        $message = preg_replace($replacearray,"<img src=\"".PIC_URL."heimindan.jpg\"/>",$message);
        //echo $message;*/
        $allow = array();
        $allow['picwidth'] = 500; //picwidth, picheight　这两个参数已经没有意义了，只要大于０，都一样
        $allow['picheight'] = '';
        $allow['size'] = 8;

        $code_num=0;
        $code_htm=array();

        $disallowed_tags = ',' . trim(strtolower($disallowed_tags), ',') . ',';

        $message = str_replace(array('expression', '*/'), array('expressiοn', '*/ '), $message); //不允许用expression
        $message = preg_replace('/\[list=([aA1]?)\](.+?)\[\/list\]/is', "<ol type=\\1>\\2</ol>", $message);
        $message = str_replace("[list]","<ol style=\"list-style-type:disc;\">",$message);
        $message = str_replace('[*]', '<li>', $message);
        $message = str_replace("[/list]","</ol>",$message);
        $message = str_replace("[u]","<u>",$message);
        $message = str_replace("[/u]","</u>",$message);
        $message = str_replace("[b]","<b>",$message);
        $message = str_replace("[/b]","</b>",$message);
        $message = str_replace("[i]","<i>",$message);
        $message = str_replace("[/i]","</i>",$message);
        $message = str_replace("[indent]","<blockquote>",$message);
        $message = str_replace("[/indent]","</blockquote>",$message);
        $message = str_replace("[/font]","</font>",$message);
        $message = str_replace("[/color]","</font>",$message);
        $message = str_replace("[/bgcolor]","</span>",$message);
        $message = str_replace("[/size]","</font>",$message);
        $message = str_replace("[/fly]","</marquee>",$message);
        $message = str_replace("[/move]","</marquee>",$message);
        $message = str_replace("[/align]","</p>",$message);
        $message = str_replace("[/glow]","</span>",$message);
        $message = str_replace("[/shadow]","</span>",$message);

        $searcharray = array(
            "/\[font=([^\[]{1,11})\]/is",
            "/\[color=([^\[\<]+?)\]/is",
            "/\[bgcolor=([^\[\<]+?)\]/is",
            "/\[size=(\d+)\]/eis",
            "/\[size=(\d+(px|pt|in|cm|mm|pc|em|ex|%)+?)\]/i",
            "/\[align=([^\[\<]+?)\]/i",
            "/\[url\](http:\/\/[-\d\w._~]{1,16}\.)(51\.com|51img1\.com)([^\[\"']*)\[\/url\]/ie",
            "/\[url=(http:\/\/)?([-\d\w._~]{1,16})\.(51\.com|51img1\.com)([^\[\"']*)\](.+?)\[\/url\]/is",
            "/\[email\]\s*([a-z0-9\-_.+]+)@([a-z0-9\-_]+[.][a-z0-9\-_.]+)\s*\[\/email\]/i",
            "/\[email=([a-z0-9\-_.+]+)@([a-z0-9\-_]+[.][a-z0-9\-_.]+)\](.+?)\[\/email\]/is",
            "/\[glow=([#0-9a-zA-Z]+?)[x|\,](\d{1,3})[x|\,](\d{1,3})\]/is",
            "/\[glow=([^\[\<]+?)\]/is",
            "/\[shadow=([#0-9a-zA-Z]+?)[x|\,](\d{1,3})[x|\,](\d{1,3})\]/is",
            "/\[shadow=([^\[\<]+?)\]/is"
        );
        if ($convert_text) {
            $replacearray = array(
                "<font face=\"\\1\">",
                "<font color=\"\\1\">",
                "<span style=\"background-color:\\1\">",
                "self::size('\\1','$allow[size]')",
                "<font style=\"font-size: \\1\">",
                "<p align=\"\\1\">",
                "self::cuturl('\\1\\2\\3')",
                "<a href=\"http://\\2.\\3\\4\" target=\"_blank\"><u>\\5</u></a>",
                "<a href=\"mailto:\\1@\\2\" style=\"color:#00c\">\\1@\\2</a>",
                "<a href=\"mailto:\\1@\\2\" style=\"color:#00c\">\\3</a>",
                "<span style=\"filter:glow(color=\\1,strength=2); width: \\2px; height: \\3px\">",
                "<span style=\"width:100%;filter:glow(color=\\1,strength=2)\">",
                "<span style=\"filter:shadow(color=\\1,direction=225); width: \\2px; height: \\3px\">",
                "<span style=\"width:100%;filter:shadow(color=\\1, direction=225)\">"
            );

            if (strpos($disallowed_tags, ',a,') !== false) { //去除所有形式的超链接。注意，以上两个数组的元素位置如有变动，则此处也应做相应调整
                $replacearray[6] = "self::cuturl('\\1\\2/\\3', true)";
                $replacearray[7] = "\\5";
                $replacearray[8] = "\\1@\\2";
                $replacearray[9] = "\\3(\\1@\\2)";
            }
			
            $message = preg_replace($searcharray,$replacearray,$message);
            $message = preg_replace("/\[img\]\s*http:\/\/pic.51.com\/user\/face\/(\d{1,3}).gif\s*\[\/img\]/is", '<img name="mms_img" src="http://pic.51.com/user/face/\\1.gif">',$message);

            $message = preg_replace("/\[img\](face\d\/\d+).gif\[\/img\]/", ' <img src="http://pic.51.com/i/face/\\1.gif">', $message);

            if (strpos($disallowed_tags, ',big_img,') !== false) {//将图片转为小图, 引用回复中用到
                $message = preg_replace("/\[img\](.+?)\[\/img\]/is","<img src='http://pic.51.com/i/comm/img.gif'>",$message);
                $message = preg_replace("/\[img=http(.*?)\]/is","<img src='http://pic.51.com/i/comm/img.gif'>",$message);
            } else {
                $message = preg_replace("/\[img\]([^\[]+?)(51\.com|51img1\.com)\/([^\[]*)\[\/img\]/eis","self::cvpic('\\1\\2/\\3','','$allow[picwidth]','$allow[picheight]')",$message);
                $message = preg_replace("/\[img=([^\]]+?)(51\.com|51img1\.com)\/([^\]]*)\]/is","<br /><img name=\"mms_img\" src=\"\\1\\2/\\3\" border=\"0\" onload=\"if(this.width>screen.width*0.7) {this.resized=true; this.width=screen.width*0.7; this.alt='点击查看大图'; this.title='点击查看大图';}\" onmouseover=\"if(this.width>screen.width*0.7) {this.resized=true; this.width=screen.width*0.7; this.style.cursor='hand'; this.alt='点击查看大图'; this.title='点击查看大图';}\" onclick=\"if(!this.resized) {return true;} else {window.open('\\1\\2/\\3');}\" ><br />",$message);
            }

            $message = preg_replace("/\[img=(\d{1,3}).gif\]/is","<img name=\"mms_img\" src=\"http://pic.51.com/user/face/\\1.gif\">",$message);

            $message = preg_replace("/\[img=(\d{1,3})[x|\,](\d{1,3})\]\s*([^\[\<\r\n]+?)\s*\[\/img\]/eis","self::bbcodeurl('\\3', '<img name=\"mms_img\" width=\"\\1\" height=\"\\2\" src=\"%s\" border=\"0\" onclick=\"window.open(\'%s\')\" alt=\"点击查看大图\" title=\"点击查看大图\" />')",$message);

            if (strpos($message,"[quote]") !== false && strpos($message,"[/quote]") !== false){
                $message=preg_replace("/\[quote\](.+?)\[\/quote\]/eis","self::qoute('\\1')",$message);
            }
            if(is_array($code_htm)){
                krsort($code_htm);
                foreach($code_htm as $codehtm){
                    foreach($codehtm as $key=>$value){
                        $message=str_replace("<\tcode_$key\t>",$value,$message);
                    }
                }
            }

            if (strpos($disallowed_tags, ',avatar,') !== false) { //去除avatar
            } else {//解析avatar
                $message = preg_replace("/\[avatar\]([0-9a-zA-Z=+\/]{1,150})\[\/avatar\]/", "<script language='javascript'>avatarImgShowSwitch(\"\\1\");</script>", $message);
            }

            if (strpos($disallowed_tags, ',movies,') !== false) { //去除影音
                $message = preg_replace(array("/(\[flash=)(\d+?)(\,)(\d+?)(\])(.+?)(\[\/flash\])/is",
                                                               "/\[movie=(.*?\.mp3|.*?\.wma)\]/i",
                                                               "/\[movie=(.*?\.asf|.*?\.avi|.*?\.wav|.*?\.mpg|.*?\.mpeg|.*?\.wm|.*?\.asx|.*?\.wvx|.*?\.wmv)\]/i",
                                                               "/\[movie=(.*?\.rm|.*?\.rmvb|.*?\.ram)\]/i",
                                                                "/\[movie=(.*?\.swf)\]/i"),
                                                      "<p>&nbsp;</p>",
                                                      $message);
            } else {
                $message = preg_replace("/(\[flash=)(\d+?)(\,)(\d+?)(\])(.+?)(\[\/flash\])/is","<OBJECT CLASSID=\"clsid:D27CDB6E-AE6D-11cf-96B8-444553540000\" WIDTH=\\2 HEIGHT=\\4><PARAM NAME=MOVIE VALUE=\\6><PARAM NAME=PLAY VALUE=TRUE><PARAM NAME=LOOP VALUE=TRUE><PARAM NAME=QUALITY VALUE=HIGH><PARAM NAME=allowFullScreen VALUE=false><param name=\"wmode\"   value=\"transparent\"><PARAM NAME=allownetworking VALUE=internal><PARAM NAME=allowscriptaccess VALUE=never><EMBED SRC=\\6 WIDTH=\\2 HEIGHT=\\4 PLAY=TRUE LOOP=TRUE QUALITY=HIGH allowscriptaccess='never' allownetworking='internal' allowFullScreen='false'></EMBED></OBJECT><br />[<a target=_blank href=\\6>全屏播放</a>]",$message);

                $message = preg_replace("/\[movie=(.*?\.mp3|.*?\.wma)\]/i","<center><object id='mPlayer1' width=468 height=64 classid='CLSID:6BF52A52-394A-11D3-B153-00C04F79FAA6'><param name='invokeURLs' value='0'><param name='URL' value='\\1'><param name='rate' value='1'><param name='balance' value='0'><param name='currentPosition' value='0'><param name='defaultFrame' value><param name='playCount' value='100'><param name='autoStart' value='true'><param name='currentMarker' value='0'><param name='invokeURLs' value='-1'><param name='baseURL' value><param name='volume' value='100'><param name='mute' value='0'><param name='uiMode' value='full'><param name='stretchToFit' value='0'><param name='windowlessVideo' value='0'><param name='enabled' value='-1'><param name='enableContextMenu' value='-1'><param name='fullScreen' value='0'><param name='SAMIStyle' value><param name='SAMILang' value><param name='SAMIFilename' value><param name='captioningID' value><param name='enableErrorDialogs' value='0'><param name='_cx' value='7779'><param name='_cy' value='1693'></object><br><font style='font-size:9pt' color=black>如果不能正常播放,请下载&nbsp; <img src='http://pic.51.com/images/wmv.gif' align='absmiddle'>&nbsp;<a href='http://www.microsoft.com/windows/windowsmedia/download/default.asp'>Windows Media Player</a></font></center>",$message);

                $message = preg_replace("/\[movie=(.*?\.asf|.*?\.avi|.*?\.wav|.*?\.mpg|.*?\.mpeg|.*?\.wm|.*?\.asx|.*?\.wvx|.*?\.wmv)\]/i","<center><object classid='clsid:22D6F312-B0F6-11D0-94AB-0080C74C7E95' height='400'  id='MediaPlayer1' width='550' ><param name='AudioStream' value='-1'><param name='AutoSize' value='0'><param name='AutoStart' value='true'><param name='AnimationAtStart' value='-1'><param name='AllowScan' value='-1'><param name='AllowChangeDisplaySize' value='-1'><param name='AutoRewind' value='-1'><param name='Balance' value='0'><param name='BufferingTime' value='5'><param name='ClickToPlay' value='-1'><param name='CursorType' value='0'><param name='CurrentPosition' value='-1'><param name='CurrentMarker' value='0'><param name='DisplayBackColor' value='0'><param name='DisplayForeColor' value='16700215'><param name='DisplayMode' value='0'><param name='DisplaySize' value='2'><param name='Enabled' value='-1'><param name='EnableContextMenu' value='1'><param name='EnablePositionControls' value='-1'><param name='EnableFullScreenControls' value='0'><param name='EnableTracker' value='-1'><param name='Filename' value='\\1'><param name='InvokeURLs' value='-1'><param name='Language' value='-1'><param name='Mute' value='0'><param name='PlayCount' value='1'><param name='PreviewMode' value='0'><param name='Rate' value='1'><param name='SelectionStart' value='-1'><param name='SelectionEnd' value='-1'><param name='SendOpenStateChangeEvents' value='-1'><param name='SendWarningEvents' value='-1'><param name='SendErrorEvents' value='-1'><param name='SendKeyboardEvents' value='0'><param name='SendMouseClickEvents' value='0'><param name='SendMouseMoveEvents' value='0'><param name='SendPlayStateChangeEvents' value='-1'><param name='ShowCaptioning' value='0'><param name='ShowControls' value='-1'><param name='ShowAudioControls' value='-1'><param name='ShowDisplay' value='0'><param name='ShowGotoBar' value='0'><param name='ShowPositionControls' value='-1'><param name='ShowStatusBar' value='-1'><param name='ShowTracker' value='-1'><param name='TransparentAtStart' value='0'><param name='VideoBorderWidth' value='0'><param name='VideoBorderColor' value='0'><param name='VideoBorder3D' value='0'><param name='Volume' value='-40'><param name='WindowlessVideo' value='0'></object><br><font style='font-size:9pt' color=black>如果不能正常播放,请下载&nbsp; <img src='http://pic.51.com/images/wmv.gif' align='absmiddle'>&nbsp;<a href='http://www.microsoft.com/windows/windowsmedia/download/default.asp'>Windows Media Player</a></font></center>",$message);


                $message = preg_replace("/\[movie=(.*?\.rm|.*?\.rmvb|.*?\.ram)\]/i","<center><object ID='video1' CLASSID='clsid:CFCDAA03-8BE4-11CF-B84B-0020AFBBCCFA' HEIGHT='400' WIDTH='550'><param name='_ExtentX' value='22304'><param name='_ExtentY' value='14288'><param name='AUTOSTART' value='true'><param name='SHUFFLE' value='0'><param name='PREFETCH' value='0'><param name='NOLABELS' value='0'><param name='SRC' value='\\1'><param name='CONTROLS' value='ImageWindow'><param name='CONSOLE' value='Clip1'><param name='LOOP' value='0'><param name='NUMLOOP' value='0'><param name='CENTER' value='0'><param name='MAINTAINASPECT' value='0'><param name='BACKGROUNDCOLOR' value='#000000'></object><object ID='video1' CLASSID='clsid:CFCDAA03-8BE4-11CF-B84B-0020AFBBCCFA' HEIGHT='60' WIDTH='400'><param name='controls' value='ControlPanel,StatusBar'><param name='console' value='Clip1'><embed type='audio/x-pn-realaudio-plugin' CONSOLE='Clip1' CONTROLS='ControlPanel,StatusBar' HEIGHT='60' WIDTH='400' AUTOSTART='true'></object></center>",$message);

                $message = preg_replace("/\[movie=(.*?\.swf)(.*?)\]/i","<center><object classid=\"clsid:D27CDB6E-AE6D-11cf-96B8-444553540000\" codebase=\"http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,40,0\" width=\"520\" height=\"402\"><param name=\"SRC\" value=\"\\1\\2\"><param name='quality' value='high'><param name='allowscriptaccess' value='never'><param name='allownetworking' value='internal'><param name='allowfullscreen' value='false'><param name='wmode' value='transparent'><embed src=\"\\1\\2\" quality='high' allowscriptaccess='never' allownetworking='internal' allowFullScreen='false' pluginspage='http://www.macromedia.com/shockwave/download/index.cgi?P1_Prod_Version=ShockwaveFlash' type='application/x-shockwave-flash' width='520' height='402' wmode='transparent'></embed></object></center>",$message);
            }

            $searcharray = array(
                "/\[fly\]/is",
                "/\[move\]/is",
                "/\s*\[table(=(\d{1,3}%?))?\][\n\r]*(.+?)[\n\r]*\[\/table\]\s*/ies",
                "/\s*\[table(=(\d{1,3}%?))?\][\n\r]*(.+?)[\n\r]*\[\/table\]\s*/ies"
            );
            $replacearray = array(
                "<marquee width=90% behavior=alternate scrollamount=3>",
                "<marquee scrollamount=3>",
                "self::parsetable('\\2', '\\3')",
                "self::parsetable('\\2', '\\3')"
            );
            $message=preg_replace($searcharray,$replacearray,$message);
            $message=preg_replace("/&amp;([#0-9]{1,6});/is","&\\1;",$message);
            //$time_end = microtime_float();
        	//$time = $time_end - $time_start;
			//echo "Did nothing in $time seconds\n";
            return $message;
        } else {
            $replacearray = array(
                "<font face=\"\\1\">",
                "<font color=\"\\1\">",
                "<span style=\"background-color:\\1\">",
                "self::size('\\1','$allow[size]')",
                "<font style=\"font-size: \\1\">",
                "<p align=\"\\1\">",
                "",
                "",
                "",
                "",
                "",
                "<span style='filter:glow(color=\\1,strength=2); width: \\2px; height: \\3px'>",
                "<span style='width:100%;filter:glow(color=\\1,strength=2)'>",
                "<span style='filter:shadow(color=\\1,direction=225); width: \\2px; height: \\3px'>",
                "<span style='width:100%;filter:shadow(color=\\1, direction=225)'>"
            );

            if (strpos($disallowed_tags, ',a,') !== false) { //去除所有形式的超链接。注意，以上两个数组的元素位置如有变动，则此处也应做相应调整
                $replacearray[6] = "self::cuturl('\\1\\2/\\3', true)";
                $replacearray[7] = "\\5";
                $replacearray[8] = "\\1@\\2";
                $replacearray[9] = "\\3(\\1@\\2)";
            }

            $message = preg_replace($searcharray,$replacearray,$message);

            $message = preg_replace("/\[img\]\s*http:\/\/pic.51.com\/user\/face\/(\d{1,3}).gif\s*\[\/img\]/is", '',$message);

            $message = preg_replace("/\[img\](.+?)\[\/img\]/is","",$message);

            $message = preg_replace("/\[img=(\d{1,3}).gif\]/is","",$message);

            $message = preg_replace("/\[img=(\d{1,3})[x|\,](\d{1,3})\]\s*([^\[\<\r\n]+?)\s*\[\/img\]/is","",$message);

            $message = preg_replace("/\[img=(.*?)\]/is","",$message);

            if (strpos($message,"[quote]") !== false && strpos($message,"[/quote]") !== false){
                $message=preg_replace("/\[quote\](.+?)\[\/quote\]/eis","self::qoute('\\1')",$message);
            }
            if(is_array($code_htm)){
                krsort($code_htm);
                foreach($code_htm as $codehtm){
                    foreach($codehtm as $key=>$value){
                        $message=str_replace("<\tcode_$key\t>",$value,$message);
                    }
                }
            }

            if (strpos($disallowed_tags, ',movies,') !== false) { //去除影音
                $message = preg_replace(array("/(\[flash=)(\d+?)(\,)(\d+?)(\])(.+?)(\[\/flash\])/is",
                                                               "/\[movie=(.*?\.mp3|.*?\.wma)\]/i",
                                                               "/\[movie=(.*?\.asf|.*?\.avi|.*?\.wav|.*?\.mpg|.*?\.mpeg|.*?\.wm|.*?\.asx|.*?\.wvx|.*?\.wmv)\]/i",
                                                               "/\[movie=(.*?\.rm|.*?\.rmvb|.*?\.ram)\]/i",
                                                                "/\[movie=(.*?\.swf)\]/i"),
                                                      "<p>&nbsp;</p>",
                                                      $message);
            } else {
                $message = preg_replace("/(\[flash=)(\d+?)(\,)(\d+?)(\])(.+?)(\[\/flash\])/is","",$message);

                $message = preg_replace("/\[movie=(.*?\.mp3|.*?\.wma)\]/i","",$message);

                $message = preg_replace("/\[movie=(.*?\.asf|.*?\.avi|.*?\.wav|.*?\.mpg|.*?\.mpeg|.*?\.wm|.*?\.asx|.*?\.wvx|.*?\.wmv)\]/i","",$message);

                $message = preg_replace("/\[movie=(.*?\.rm|.*?\.rmvb|.*?\.ram)\]/i","",$message);

                $message = preg_replace("/\[movie=(.*?\.swf)\]/i","",$message);
            }

            $searcharray = array(
                "/\[fly\]/is",
                "/\[move\]/is",
                "/\s*\[table(=(\d{1,3}%?))?\][\n\r]*(.+?)[\n\r]*\[\/table\]\s*/is",
                "/\s*\[table(=(\d{1,3}%?))?\][\n\r]*(.+?)[\n\r]*\[\/table\]\s*/is"
            );
            $replacearray = array(
                "<marquee width=90% behavior=alternate scrollamount=3>",
                "<marquee scrollamount=3>",
                "",
                ""
            );
            $message=preg_replace($searcharray,$replacearray,$message);
            $message=preg_replace("/&amp;([#0-9]{1,6});/is","&\\1;",$message);
            return $message;
        }
    }
    static function fontSize($size)
    {
        if ($size < FONT_MIN_SIZE)
        {
            $size = FONT_MIN_SIZE;
        }
        if ($size > FONT_MAX_SIZE)
        {
            $size = FONT_MAX_SIZE;
        }
        return "<font size=\"$size\">";
    }
    static function cvPic($url){
        $url = str_replace(array("&#39;","'"),'',$url);
        $turl = $url;
        $alt = '';
        #$wopen = !$wopen ? "if(this.width>=$picwidth)" : '';
        $onload = 'onload="';
        $onload .= "if(this.width>".IMG_MAX_WIDTH.")this.width=".IMG_MAX_WIDTH.";";
        $onload .= "if(this.height>".IMG_MAX_HEIGHT.")this.height=".IMG_MAX_HEIGHT.";";
        $onload .= '"';
        $code = "<img src=\"$turl\" border=\"0\" onclick=\"window.open('$url');\" $onload $alt>";
        return $code;
    }

	static function resizePic($url){
		$url = str_replace(array("&#39;","'"),'',$url);
		$alt = '';
		/**$temp=@getimagesize($url);
		if (is_array($temp)) 
		{
			$temp[0] =intval($temp[0]);
			$temp[1] =intval($temp[1]);
			$rat =$temp[0]/$temp[1];
			if ($temp[0]>600) 
			{
				$width = $temp[0]/4;
				$height = $temp[1]/(4*$rat);
			}
			elseif ($temp[0]>300)
			{
				$width = $temp[0]/2;
				$height = $temp[1]/(2*$rat);
			}
			else
			{
				$width = $temp[0];
				$height = $temp[1];
			}
		}*/
		$onload = 'onload="';
		$onload .= " if(this.width>506) {this.width=506;this.height = this.height/(this.width/506*(this.width/this.height));}";
		$onload .= " if(this.height>280) {this.height=280;this.width = this.width/(this.height/280*(this.height/this.width));}";
		$onload .= '"';
		//$code = "<img src=\"$url\" border=\"0\" onclick=\"window.open('$url');\"  $onload $alt>";
		$code = "<img src=\"$url\" border=\"0\"  $onload $alt>";
		return $code;
	}
    static function cvUrl($http,$url='',$name='')
    {
        if(!$url)
        {
            $url = "<a href=\"http://www.$http\" target=\"_blank\">www.$http</a>";
        }
        elseif(!$name)
        {
            $url = "<a href=\"$http$url\" target=\"_blank\">$http$url</a>";
        }
        else
        {
            $url = "<a href=\"$http$url\" target=\"_blank\">".str_replace('\\"','"',$name)."</a>";
        }
        return $url;
    }
    static function qoute($code)
    {
        return "<blockquote class=\"blockquote\">".str_replace('\\"','"',$code)."</blockquote>";
    }
}
?>